package edu.sandau.app.basic.service.impl;

import edu.sandau.app.basic.domain.Comment;
import edu.sandau.app.basic.domain.CommentExample;
import edu.sandau.app.basic.mapper.CommentMapper;
import edu.sandau.app.basic.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    
    @Autowired
    private CommentMapper commentMapper;
    
    @Override
    public List<Comment> getAllComments() {
        CommentExample example = new CommentExample();
        example.setOrderByClause("create_time DESC");
        return commentMapper.selectByExampleWithBLOBs(example);
    }
    
    @Override
    public Comment getCommentById(Long id) {
        return commentMapper.selectByPrimaryKey(id);
    }
    
    @Override
    @Transactional
    public int addComment(Comment comment) {
        comment.setCreateTime(new Date());
        comment.setUpdateTime(new Date());
        if (comment.getCommentStatus() == null) {
            comment.setCommentStatus("0"); // 默认状态为正常
        }
        if (comment.getRating() == null) {
            comment.setRating(5); // 默认5星
        }
        return commentMapper.insertSelective(comment);
    }
    
    @Override
    @Transactional
    public int updateComment(Comment comment) {
        comment.setUpdateTime(new Date());
        return commentMapper.updateByPrimaryKeySelective(comment);
    }
    
    @Override
    @Transactional
    public int deleteComment(Long id) {
        return commentMapper.deleteByPrimaryKey(id);
    }
    
    @Override
    public List<Comment> getCommentsByDishId(Long dishId) {
        CommentExample example = new CommentExample();
        example.createCriteria().andDishIdEqualTo(dishId).andCommentStatusEqualTo("0");
        example.setOrderByClause("create_time DESC");
        return commentMapper.selectByExampleWithBLOBs(example);
    }
    
    @Override
    public List<Comment> getCommentsByUserId(Long userId) {
        CommentExample example = new CommentExample();
        example.createCriteria().andUserIdEqualTo(userId);
        example.setOrderByClause("create_time DESC");
        return commentMapper.selectByExampleWithBLOBs(example);
    }
    
    @Override
    public List<Comment> getCommentsByOrderId(Long orderId) {
        CommentExample example = new CommentExample();
        example.createCriteria().andOrderIdEqualTo(orderId);
        example.setOrderByClause("create_time DESC");
        return commentMapper.selectByExampleWithBLOBs(example);
    }
    
    @Override
    @Transactional
    public int toggleCommentStatus(Long id, String status) {
        Comment comment = commentMapper.selectByPrimaryKey(id);
        if (comment == null) {
            return 0;
        }
        comment.setCommentStatus(status);
        comment.setUpdateTime(new Date());
        return commentMapper.updateByPrimaryKeySelective(comment);
    }
}

